package com.aid.winter.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.aid.winter.common.response.Result;
import com.aid.winter.dto.request.LoginRequest;
import com.aid.winter.dto.request.PasswordUpdateRequest;
import com.aid.winter.dto.response.LoginVO;
import com.aid.winter.entity.SysUser;
import com.aid.winter.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class SysUserController {

    private final SysUserService userService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<LoginVO> login(@Validated @RequestBody LoginRequest request) {
        return Result.ok(userService.login(request));
    }

    /**
     * 获取当前用户信息
     */
    @GetMapping("/current")
    public Result<SysUser> getCurrentUser() {
        return Result.ok(userService.getCurrentUser());
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public Result<Void> logout() {
        userService.logout();
        return Result.ok();
    }
    
    /**
     * 修改密码
     */
    @PutMapping("/password")
    public Result<Void> updatePassword(@Validated @RequestBody PasswordUpdateRequest request) {
        Long userId = StpUtil.getLoginIdAsLong();
        userService.updatePassword(userId, request);
        return Result.ok();
    }
}